<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="关怀标题" prop="title">
				<el-input v-model="form.title" placeholder="关怀标题" clearable maxlength="80"></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="关怀类型" prop="careType">
						<sc-select v-model="form.careType" :params="{code:'careType'}" :apiObj="$API.crm.crmDic.getCrmDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="审批人" prop="approvedUser">
						<el-select v-model="form.approvedUser" clearable filterable style="width: 100%">
							<el-option v-for="item in userList" :key="item.accountId" :label="item.userName" :value="item.accountId"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="客户名称" prop="customerId">
						<el-select
							v-model="form.customerId"
							filterable
							remote
							reserve-keyword
							placeholder="请输入关键词"
							:remote-method="remoteMethod"
							:loading="loading"
							style="width:100%"
						>
							<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="相关人员" prop="linkManId">
						<sc-select v-model="form.linkManId" :params="{customerId:form.customerId}" :apiObj="$API.crm.crmLinkMan.getCrmLinkManListForSelect" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="开始时间" prop="careTime" maxlength="80">
						<el-date-picker :disabled-date="publishDateAfter" v-model="form.careTime" placeholder="开始时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="关怀预算" prop="careFunds">
						<el-input-number v-model="form.careFunds" placeholder="关怀预算" clearable></el-input-number>/元
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="关怀备注" >
				<el-input type="textarea" rows="4" v-model="form.remark" placeholder="关怀备注" clearable maxlength="380"></el-input>
			</el-form-item>
			<el-form-item label="相关附件" prop="attachId">
				<sc-upload-file :apiObj="apiObj"  v-model="fileList" :data="{module:'crm'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>

import AttachMenu from "@/views/module/attachMenu";

export default {
	emits: ['success', 'closed'],
	components: {
		AttachMenu
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			isShowMsg:false,
			loading:false,
			userList:[],
			fileList:[],
			mode: "add",
			apiObj:this.$API.file.attach.uploadFile,
			titleMap: {
				add: '创建关怀记录',
				edit: '编辑关怀记录',
			},
			form: {
				recordId:'',
				title: '',
				careType: '',
				customerId: '',
				linkManId:'',
				careTime:'',
				careFunds: '',
				remark: '',
				attachId:''
			},
			options: [],
			rules: {
				title: [
					{required: true, message: '关怀标题不能为空', trigger: 'blur'}
				],
				careType:[
					{required: true, message: '关怀类型不能为空', trigger: 'blur'}
				],
				customerId:[
					{required: true, message: '客户名称不能为空', trigger: 'blur'}
				],
				linkManId:[
					{required: true, message: '相关人员不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
		this.getCrmApprovedUserList();
	},
	methods: {
		async getCrmApprovedUserList()
		{
			let res = await this.$API.crm.crmRole.getCrmApprovedUserList.get();
			if (res.code == 200) {
				this.userList = res.data;
			}
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		publishDateAfter(time){
			return time.getTime()<(Date.now()-24*60*60*1000)
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.crm.crmCare.insertCrmCare.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.crm.crmCare.updateCrmCare.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		addNewFile(res, file)
		{
			let tArr =[];
			if(this.form.attachId!=undefined&&this.form.attachId!=null&&this.form.attachId!=''){
				tArr = this.form.attachId.split(",");
			}
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		},
		async getAllCustomerListForSelect(customerId) {
			let res = await this.$API.crm.crmCustomer.getAllCustomerListForSelect.get({customerId: customerId});
			if (res.code == 200) {
				this.options = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async remoteMethod(keyword) {
			let res = await this.$API.crm.crmCustomer.getAllCustomerListForSelect.get({keyword: keyword});
			if (res.code == 200) {
				this.options = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		setData(data) {
			this.getAllCustomerListForSelect(data.customerId);
			this.form.recordId = data.recordId
			this.form.title = data.title
			this.form.careType = data.careType
			this.form.attachId = data.attachId
			this.form.customerId = data.customerId
			this.form.linkManId = data.linkManId
			this.form.careTime = data.careTime
			this.form.careFunds = data.careFunds
			this.form.remark = data.remark
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
